package com.mystore.dao.impl;

import com.mystore.dao.BaseDao;
import com.mystore.dao.CartDao;
import com.mystore.entity.Cart;
import com.mystore.vo.GoodVO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {

    private BaseDao baseDao;

    public CartDaoImpl() {
        this.baseDao =new BaseDaoImpl();
    }

    @Override
    public List<GoodVO> findAllMyCartByUid(int uid, Connection connection) {
        String sql = "select c.id,g.name,g.msg,g.price,c.gid,c.num from good g,cart c  where c.uid=? and g.id=c.gid";
        List<Object> list = new ArrayList<>();
        list.add(uid);
        List<GoodVO> goodVOList = null;
        try {
            goodVOList= baseDao.findMoreRefResult(sql,list,GoodVO.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goodVOList;
    }

    @Override
    public Boolean updateCart(Cart cart,Connection connection) throws SQLException {
        String sql = "update  cart set num =? where uid=? and gid=?";
        List<Object> list = new ArrayList<>();
        list.add(cart.getNum());
        list.add(cart.getUid());
        list.add(cart.getGid());

        return baseDao.updateByPreparedStatement(sql,list,connection);
    }

    @Override
    public Cart findByUidAndGid(int uid, int gid, Connection connection) {
        String sql = "select * from cart where uid=? and gid= ?";
        List<Object> list = new ArrayList<>();
        list.add(uid);
        list.add(gid);
        Cart cart = null;
        try {
           cart  = (Cart) baseDao.findSingleResult(sql,list,Cart.class,connection);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cart;
    }

    @Override
    public Boolean save(Cart cart,Connection connection) throws SQLException {
        String sql = "insert into cart(uid,gid,num) values(?,?,?)";
        List<Object> list = new ArrayList<>();
        list.add(cart.getUid());
        list.add(cart.getGid());
        list.add(cart.getNum());
        return baseDao.updateByPreparedStatement(sql,list,connection);
    }
}
